package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.FreeHed;
import com.datamation.swadeshi.model.FreeItem;

public class FreeItemDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public FreeItemDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	@SuppressWarnings("static-access")
	public int createOrUpdateFreeItem(ArrayList<FreeItem>  list) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
					
    for (FreeItem item : list) {
		
			ContentValues values = new ContentValues();
			
			values.put(dbHelper.FFREEITEM_ITEMCODE,item.getFFREEITEM_ITEMCODE());
			values.put(dbHelper.FFREEITEM_REFNO, item.getFFREEITEM_REFNO());
			values.put(dbHelper.FFREEITEM_RECORD_ID, item.getFFREEITEM_RECORD_ID());
								
			count = (int) dB.insert(dbHelper.TABLE_FFREEITEM, null, values);

			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	@SuppressWarnings("static-access")
	public int deleteAll(){
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FFREEITEM, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FFREEITEM, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
	}
	
	
	public ArrayList<FreeItem> getFreeItemsByRefno(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FreeItem> list = new ArrayList<FreeItem>();
		
		String selectQuery = "select * from ffreeItem where refno='"+refno+"'";
		
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			FreeItem item=new FreeItem();
		
			item.setFFREEITEM_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEITEM_ID)));
			item.setFFREEITEM_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEITEM_REFNO)));
			item.setFFREEITEM_ITEMCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEITEM_ITEMCODE)));
			item.setFFREEITEM_RECORD_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEITEM_RECORD_ID)));
				
			list.add(item);
			
		}
		
		return list;
	}
}
